'use strict';

const Controller = require('egg').Controller;

class CollectController extends Controller {
    async index() {
        const { ctx } = this;
        ctx.body = "collect"
        console.log(ctx.request.body);
        var { id, collected } = ctx.request.body;
        collected = (collected == "true") ? true : false;
        console.log(collected);
        var db = this.app.mongodb;
        var res = await ctx.service.movie.getMovieDetailOrSearch({ id: id ,collected: collected})
        console.log(res);
        try {
            if (collected) {
                await db.collection("user").insertOne(res[0]);
                ctx.body = {
                    status: 200,
                    msg: "收藏成功"
                }
            } else {
                await db.collection("user").deleteOne({ _id: id });
                ctx.body = {
                    status: 200,
                    msg: "取消收藏"
                }
            }
        } catch (err) {
            this.app.logger.error(err)
            ctx.body = {
                status: 400,
                msg: "数据已经存在"
            }
        }
    }
}

module.exports = CollectController;
